﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.MembershipProviders;


/// <summary>
/// Selection user control that represents hierarchical structure of Projects an releases
/// </summary>
public partial class ReleaseSelection : HierachicalObjectSelection
{

    /// <summary>
    /// Event triggered when release changes
    /// </summary>
    public event EventHandler SelectedReleaseChangedEvent;

    /// <summary>
    /// Event triggered when project changes
    /// </summary>
    public event EventHandler SelectedProjectChangedEvent;
    
    /// <summary>
    /// Gets the selected release.
    /// </summary>
    /// <value>The selected release.</value>
    public Release SelectedRelease
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListRelease.SelectedValue) || this.DropDownListRelease.SelectedValue == "-1")
            {
                return null;
            }
            else
            {
                return DAORelease.GetReleaseById(Int32.Parse(this.DropDownListRelease.SelectedValue));
            }
        }
    }

    /// <summary>
    /// Selected release ID
    /// </summary>
    public int SelectedReleaseID
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListRelease.SelectedValue) || this.DropDownListRelease.SelectedValue == "-1")
            {
                return -1;
            }
            else
            {
                return Int32.Parse(this.DropDownListRelease.SelectedValue);
            }
        }
    }

    /// <summary>
    /// Selected project ID
    /// </summary>
    public int SelectedProjectID
    {
        get
        {
            return ProjectSelectionRelease.SelectedProjectID;
        }
    }

    /// <summary>
    /// Sets the release ID.
    /// </summary>
    /// <param name="releaseID">The release ID.</param>
    public void SetReleaseID(int releaseID)
    {
        ListItem li = DropDownListRelease.Items.FindByValue(releaseID.ToString());
        
        DropDownListRelease.SelectedIndex = releaseID;
        if (SelectedProjectChangedEvent != null)
        {
            SelectedProjectChangedEvent(this, new EventArgs());
        }

        li.Selected = true;
        
    }

    /// <summary>
    /// Gets the selected project.
    /// </summary>
    /// <value>The selected project.</value>
    public Project GetSelectedProject()
    {
        return this.ProjectSelectionRelease.SelectedProject;
    }    

    /// <summary>
    /// /// Sets handlers for events.
    /// </summary>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected override void OnInit(EventArgs e)
    {
        this.ProjectSelectionRelease.SelectedProjectChangedEvent += new EventHandler(SelectedProjectChanged);
        this.ProjectSelectionRelease.SelectedProjectChangedEvent += new EventHandler(SelectedProjectChangedKeeper);
    }

    /// <summary>
    /// It keeps projectchangedevent for SprintSelection.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedProjectChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedProjectChangedEvent != null)
            {
                this.SelectedProjectChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    /// <summary>
    /// Loads the releases.
    /// </summary>
    protected void LoadReleases()
    {
        Project p = ProjectSelectionRelease.SelectedProject;
        if (p == null) 
        {
            DropDownListRelease.DataSource = DAORelease.GetAllReleases();
        }
        else
        {
            DropDownListRelease.DataSource = DAORelease.GetAllReleasesForProject(p.ID);
        }
        DropDownListRelease.DataTextField = "Name";
        DropDownListRelease.DataValueField = "ID";
        DropDownListRelease.DataBind();
        DropDownListRelease.Items.Insert(0, TopListItemForFilter("Filters","AnyRelease"));
    }

    /// <summary>
    /// Binds a data source to the invoked server control and all its child controls.
    /// </summary>
    public override void DataBind()
    {
        base.DataBind();
        ProjectSelectionRelease.DataBind();
        LoadReleases();
    }

    /// <summary>
    /// Selected project changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedProjectChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadReleases();
        }
    }

    /// <summary>
    /// Selected release changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedReleaseChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedReleaseChangedEvent != null)
            {
                this.SelectedReleaseChangedEvent(sender, e);
            }

            if (this.SelectedRelease == null)
            {
                return;
            }
            int selectedReleaseID = this.SelectedRelease.ID;

            // Select the right project
            ProjectSelectionRelease.SetProjectByRelease(selectedReleaseID);
            //int skuska = ProjectSelectionRelease.SelectedProjectID;
            // Reload releases
            LoadReleases();

            // Select the right release
            for (int i = 0; i < this.DropDownListRelease.Items.Count; i++)
            {
                if (this.DropDownListRelease.Items[i].Value == selectedReleaseID.ToString())
                {
                    this.DropDownListRelease.SelectedIndex = i;
                    break;
                }
            }
        }
    }

    /// <summary>
    /// Sets the right project and release in their DDLs
    /// </summary>
    /// <param name="sprintID">ID of sprint</param>
    public void SetProjectReleaseBySprint(int sprintID)
    {
        Release release = DAORelease.GetReleaseBySprintID(sprintID);

        // Select the right project
        ProjectSelectionRelease.SetProjectByRelease(release.ID);

        LoadReleases();

        // Select the right release
        ListItem li = DropDownListRelease.Items.FindByValue(release.ID.ToString());

        if (li != null)
        {
            li.Selected = true;
            //this.SelectedReleaseID = release.ID;
        }
        else
        {
            this.DropDownListRelease.SelectedValue = "-1";
            //this.SelectedReleaseID = -1;
        }
        //for (int i = 0; i < this.DropDownListRelease.Items.Count; i++)
        //{
        //    if (this.DropDownListRelease.Items[i].Value == release.ID.ToString())
        //    {
        //        this.DropDownListRelease.SelectedIndex = i;
        //        break;
        //    }
        //}
    }

    /// <summary>
    /// Clears the dropdown lists.
    /// </summary>
    public void Clear(object sender, EventArgs e)
    {
        ProjectSelectionRelease.Clear(sender, e);
        //DataBind();
        //this.DropDownListProject.SelectedIndex = 0;
        //this.DropDownListTeams.SelectedIndex = 0;
    }

    protected override WebControl GetWebControl()
    {
        return this.DropDownListRelease;
    }

    protected override Label GetLabel()
    {
        return this.LabelReleases;
    }
}
